using System;
using System.ComponentModel;
using System.Collections;
using System.Collections.Specialized;
using System.Drawing.Design;
using System.Drawing;
using System.Globalization;
using System.ComponentModel.Design.Serialization;
using System.Reflection;

using SCS.Web.Controls;

namespace SCS.Web.Controls.Design
{
	internal class ContextHotSpotCollectionConverter : TypeConverter
	{
		public override bool CanConvertFrom(ITypeDescriptorContext context, Type t)
		{
			if (t == typeof(string))
				return true;

			return base.CanConvertFrom(context, t);
		}

		public override bool CanConvertTo(ITypeDescriptorContext context, Type destType) 
		{
			if (destType == typeof(InstanceDescriptor) || destType == typeof(string)) 
			{
				return true;
			}
			return base.CanConvertTo(context, destType);
		}	
		
		
		public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo info, object value)
		{
			if (value is string)
			{
				string hotSpotList = (string)value;
				
				try
				{
					ContextHotSpotCollection areaIdCollection = new ContextHotSpotCollection();

					string[] hotSpot = hotSpotList.Split(info.TextInfo.ListSeparator[0]);
					
					for (int i = 0; i < hotSpot.Length; i++)
					{
						string id = (hotSpot[i].Split(' ')[0]).Trim();
						int depth = int.Parse((hotSpot[i].Split(' ')[1]).Trim());

						areaIdCollection.Add(new ContextHotSpot(id, depth));
					}

					return areaIdCollection;
				}
				catch
				{
					return new ContextHotSpotCollection();
				}				
			}

			return base.ConvertFrom(context, info, value);
		}

		public override object ConvertTo(ITypeDescriptorContext context, CultureInfo info, object value, Type destType )
		{
			if (destType == typeof(string))
			{
				ContextHotSpotCollection areaIdCollection = (ContextHotSpotCollection)value;
				string hotSpotList = string.Empty;
				
				foreach (ContextHotSpot hotSpot in areaIdCollection)
				{
					hotSpotList += string.Format("{0} {1} {2}", 
						info.TextInfo.ListSeparator[0], hotSpot.ClientId, hotSpot.ChildDepth);
				}
				
				if (hotSpotList.Length > 0)
				{
					hotSpotList = hotSpotList.Substring(2);
				}

				return hotSpotList;
			}
			else if (destType == typeof(InstanceDescriptor)) 
			{
				// string constructor
				Type[] types = new Type[] { typeof(string) };
				ConstructorInfo constructorInfo = typeof(ContextHotSpotCollection).GetConstructor(types);
				
				ContextHotSpotCollection hotSpots = (ContextHotSpotCollection)value;
				
				string hotSpotList = string.Empty;

				foreach (ContextHotSpot hotSpot in hotSpots)
				{
					hotSpotList += string.Format("{0} {1} {2}", 
						info.TextInfo.ListSeparator[0], hotSpot.ClientId, hotSpot.ChildDepth);
				}

				if (hotSpotList.Length > 0)
				{
					hotSpotList = hotSpotList.Substring(2);
				}

				object[] args = new object[] { hotSpotList };


				InstanceDescriptor descriptor = new InstanceDescriptor(constructorInfo, args, true);

				return descriptor;
			}

			return base.ConvertTo(context, info, value, destType);
		}
	}
}
